import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/EventMgr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
///待评价订单控制器
class CommentOrderListController extends BaseController with SingleGetTickerProviderMixin{
  late TabController StatusTabController;

  RefreshController baseRefreshCtr=RefreshController(initialRefresh: false);
  RefreshController baseRefreshCtr2=RefreshController(initialRefresh: false);



  int page = 1;

  int type = 5;

  ///待评价数量
  int waitNum = 0;


  ///已评价数量
  int completeNum = 0;

  bool hasNextPage = false;
  ///订单列表
  List orderList = [];

  @override
  void onInit() {
    getCommentNum();
    getMyorderLists();
    //---------订单状态---------
    StatusTabController = TabController(length: 2, vsync: this);
    StatusTabController.addListener(() { 
      if(StatusTabController.index.toDouble()==StatusTabController.animation?.value){
        // print("切换了状态${StatusTabController.index}");
        page = 1;
        if(StatusTabController.index==0){
          type = 5;
        }else{
          type = 6;
          print("查找已完成");
        }
        getMyorderLists();

      }
    });

      EventMgr.on(EventType.refreshCommentList, (arg) {
        logger.d('评价列表刷新>>>>>>>');
        getCommentNum();
        getMyorderLists();
      
    });
    //---------------------------
    super.onInit();
  }


  //获取评价单的数量
  getCommentNum(){
    Tools.showLoading();
    Network.getCommentNum().then((value){
      waitNum = value['noCommentNum'];
      completeNum = value['commentNum'];
      update();
      Tools.hideLoading();
    }).catchError((error){
      Tools.hideLoading();
    });
  }

  //获取待评价列表
  getMyorderLists(){
    Tools.showLoading();
    Network.getMyOrderList(type,  page, 5).then((value){
      if(page==1){
        orderList = value['list'];
      }else{
        orderList.addAll(value['list']);
      }
      hasNextPage = value['hasNextPage'];
      update();
      Tools.hideLoading();
    }).catchError((error){
      Tools.hideLoading();
    });
  }


  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    page = 1;
    getMyorderLists();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
    baseRefreshCtr2.refreshCompleted(resetFooterState: true);

  }

  @override
  void onPageLoading()async {
    if(hasNextPage==true){
      super.onPageLoading();
      page += 1;
      getMyorderLists();
      baseRefreshCtr.loadComplete();
      baseRefreshCtr2.loadComplete();

    }
  }

  @override
  void onClose() {
    StatusTabController.dispose();
    super.onClose();
  }

  @override
  void dispose() {
    EventMgr.off(EventType.refreshCommentList);
    super.dispose();
  }

}
